#include <stdio.h>
#include <string.h>
#include <sqlite3.h>
#include "admin_menu.h"
#include "admin_function.h"

int admin_menu(){
 sqlite3 *db;
      if(sqlite3_open("game_users.db",&db)!=SQLITE_OK){
	      fprintf(stderr,"无法打开数据库 %s\n",sqlite3_errmsg(db));
	       return 1;
      }
      init_db(db);
      char choice[10];
      while(1){
      system:
                printf("\n===== 管理员系统 =====");
	           printf("\n1. 显示用户  2. 添加用户");
		      printf("\n3. 删除用户  4. 存档(!)");
			 printf("\n5. 重置密码  6.修改游戏数据(!)\n");
			 printf(" 0. 退出\n");
			 printf("\n请选择操作: ");
			 fgets(choice,sizeof(choice),stdin);//?
			 choice[strcspn(choice,"\n")] = 0;//去除换行符
               if(strcmp(choice,"0")==0){
	          break;
	       }
	       //显示用户列表
	       else if(strcmp(choice,"1") ==0){
	          show_users(db);
	       }
               //显示角色列表
	       else if(strcmp(choice,"4")==0){
	           printf("\n角色列表:\n");
		   const char *sql ="SELECT * FROM role";
		   sqlite3_stmt *stmt;
		   if(sqlite3_prepare_v2(db,sql,-1,&stmt,NULL) ==SQLITE_OK){
		        while(sqlite3_step(stmt) ==SQLITE_ROW){
			     printf("ID:  %d, 名称:%s, 描述:%s\n",
					     sqlite3_column_int(stmt,0),
					     (const char*)sqlite3_column_text(stmt,1),
					     (const char*)sqlite3_column_text(stmt,2));
			}
			sqlite3_finalize(stmt);
		   }else{
		    fprintf(stderr,"查询角色失败:%s\n",sqlite3_errmsg(db));
		   }
	       }
      
      		//添加用户
           else if(strcmp(choice,"2")==0){
	       char username[50],password[50],sql[512];
	       
	       printf("输入用户名: ");
	       fgets(username,sizeof(username),stdin);
	       username[strcspn(username,"\n")] =0;

	       printf("请输入密码: ");
	       fgets(password,sizeof(password),stdin);
	       password[strcspn(password,"\n")] =0;
	       
	       //构造并执行sql
	       sprintf(sql,"INSERT INTO users(username,password)"
			      " VALUES('%s', '%s')",
			      username, password);
	       if(exec_sql(db,sql)){
	       printf("用户添加成功\n");
	       }else {
	        printf("用户添加失败\n");
	       }
	   }
	       //删除用户
	       else if(strcmp(choice,"3") ==0){
	          char id[10],sql[128];
		  printf("输入要删除的用户ID: ");
		  fgets(id,sizeof(id),stdin);
		  id[strcspn(id,"\n")]  ='\0';

		  sprintf(sql,"DELETE FROM users WHERE id = %s",id);
		  if(exec_sql(db,sql)){
		  printf("用户删除成功\n");
		  }else
		  {
		   printf("用户删除失败");
		  }
	       }//重置密码
              else if(strcmp(choice,"5") ==0){
		   char id[10],new_pwd[50],sql[256];
		   printf("输入要重置密码的用户ID: ");
		   fgets(id,sizeof(id),stdin);
		   id[strcspn(id,"\n")] ='\0';

		   printf("请输入新密码: ");
		   fgets(new_pwd,sizeof(new_pwd),stdin);
		   new_pwd[strcspn(new_pwd,"\n")] ='\0';

		   sprintf(sql,"UPDATE users SET password = '%s' WHERE id = %s",new_pwd,id);
		  
	      if(exec_sql(db,sql)){
	         printf("密码重置成功\n");
	      }else{
	         printf("密码重置失败\n");
	      }
	    }
	       else if(strcmp(choice,"6")==0){
	       	int i;
	       	char i_choice[10];
	       	choice:
	       	printf("你要修改什么？\n");
	       	printf("1.装备  2.武器.  3.添加游戏主角  4.修改怪物属性  0.返回上一级\n");
	       	fgets(i_choice,sizeof(i_choice),stdin);
	       	i=atoi(i_choice);
	       	if(i==0){
	       		goto system;
	       		}
	       	switch(i){
	       		case 1:
	       			printf("暂未完成。\n");
	       			goto choice;
	       			break;
	       		case 2:
	       			printf("暂未完成。\n");
	       			goto choice;
	       			break;
	       		case 3:
	       			printf("暂未完成。\n");
	       			goto choice;
	       			break;
	       		case 4:
	       			printf("暂未完成。\n");
	       			goto choice;
	       			break;
	       		default:
	       			printf("请输入正确的选项！！！\n");
	       			goto choice;
	       			break;
	       	}
	       	
	       }
	      
	      }
	       sqlite3_close(db);
	       printf("系统已退出\n");
	       return 0;
}